package com.yy.controller.mapper;

import com.yy.entity.ShoppingCart;
import com.yy.entity.User;
import org.apache.ibatis.annotations.*;

import java.awt.peer.LabelPeer;
import java.math.BigDecimal;
import java.util.List;

@Mapper
public interface ShoppingCartMapper {
    /**
     * 根据用id获取购物车列表
     * @return
     * @param userId
     */
    @Select("select * from shopping_cart where user_id=#{userId}")
    List<ShoppingCart> list(Long userId);

    /**
     * 根据id添加到购物车
     * @param shoppingCart
     */
    @Insert("insert into shopping_cart ( name, image, user_id," +
            " dish_id, setmeal_id, " +
            " number, amount, create_time) " +
            "values(#{name},#{image},#{userId},#{dishId}," +
            "#{setmealId},#{number},#{amount},#{createTime})")
    void add(ShoppingCart shoppingCart);

    /**
     * 根据用户id删除清空购物车
     * @param userId
     */
    @Delete("delete  from shopping_cart where user_id=#{userId}")
    void clear(Long userId);

    /**
     * 根据dishId查询购物车菜品信息
     * @param dishId
     * @return
     */
    @Select("select *  from shopping_cart where dish_id=#{dishId} and user_id=#{userId}")
    ShoppingCart getByDishId(Long dishId,Long userId);

    /**
     * 通过id修改购物车信息
     * @param id
     */
    @Update("update shopping_cart set  amount=amount+#{price},number=number+1 where id=#{id}")
    void updateNumber(Long id, BigDecimal price);

    /**
     * 根据stemalId查询购物车套餐信息
     * @param setmealId
     * @return
     */
    @Select("select *  from shopping_cart where setmeal_id=#{setmealId} AND user_id=#{userId}")
    ShoppingCart getBySetmealId(Long setmealId, Long userId);

    /**
     * 根据dishId查询购物车菜品
     * @param dishId
     * @return
     */
    @Select("select *  from shopping_cart where dish_id=#{dishId} and user_id=#{userId}")
    List<ShoppingCart> getListByDishId(Long dishId,Long userId);


    @Update("update shopping_cart set amount=(amount/number)*(number-1),number=number-1 where id=#{id}")
    void reduceById(Long id);

    /**
     * 根据id获取数据
     * @param id
     * @return
     */
    @Select("select * from shopping_cart where id=#{id}")
    ShoppingCart getById(Long id);

    /**
     * 根据id删除数据
     * @param id
     */
    @Delete("delete from shopping_cart  where id=#{id}")
    void deleteById(Long id);

    /**
     * 减少菜品或套餐数量通过菜品id(无口味菜品)
     * @param dishId
     */
    @Update("update shopping_cart set  amount=(amount/number)*(number-1),number=number-1 where dish_id=#{dishId}")
    void reduceByDishId(Long dishId);

    /**
     * 减少菜品或套餐数量通过套餐id
     * @param setmealId
     */
    @Update("update shopping_cart set amount=(amount/number)*(number-1),number=number-1 where setmeal_id=#{setmealId}")
    void reduceBysetmealId(Long setmealId);

    /**
     * 增加菜品或套餐数量通过id
     * @param id
     */
    @Update("update  shopping_cart set amount=(amount/number)*(number+1),number=number+1 where id=#{id}")
    void addById(Long id);
}
